package com.app.bimo.commonbusiness.network;


import android.util.Log;

import com.app.bimo.commonbusiness.base.BaseApplication;
import com.app.bimo.commonbusiness.mvp.IView;
import com.app.bimo.commonbusiness.network.Exception.ApiException;
import com.app.bimo.commonbusiness.network.Exception.CustomException;
import com.app.bimo.commonbusiness.cache.CacheManager;
import com.app.bimo.commonbusiness.util.NetWrokUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;

public abstract class CommonSubscriber<T> extends DisposableObserver<T> {

    private IView mBaseView;
    private boolean isHideLoading = false;
    private boolean isPageLoading = false;

    private Consumer<ApiException> apiExceptionConsumer;

    private String url;
    private static Gson gson = new Gson();
    private int cacheTime = 60 * 60 * 1000;   //缓存时间
    private boolean isRefresh; //是否刷新
    private boolean isFirstLoadCache = false; // 是否先显示缓存数据

    public CommonSubscriber(String url, IView mBaseView, boolean isHideLoadin, boolean isPageLoading, Consumer<ApiException> apiExceptionConsumer) {
        this.mBaseView = mBaseView;
        this.isHideLoading = isHideLoadin;
        this.isPageLoading = isPageLoading;
        this.apiExceptionConsumer = apiExceptionConsumer;
        this.url = url;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mBaseView != null && isHideLoading) {
            mBaseView.showDialogLoading();
        }
        if (mBaseView != null && isPageLoading){
            mBaseView.showLoading();
        }

        try{
            //网络可用
            if (NetWrokUtils.isNetworkConnected(BaseApplication.getContext())){

            }else{

            }

//            //获取缓存数据    cache 为空表示没有缓存数据
//            String cache = CacheManager.getInstance().loadCache(url,false);
//
//            //先显示缓存数据&&本地有缓存&&缓存未过期
//            if (isFirstLoadCache && cache != null && System.currentTimeMillis() - CacheManager.getInstance().getCacheTime(url) < cacheTime){
//                //获取缓存bean
//                T cacheT = gson.fromJson(cache,new TypeToken<T>() {}.getType());
//                //渲染数据
//                onNext(cacheT);
//            }
//            //非强刷&&本地有缓存&&缓存未过期
//            else if (!isRefresh && cache != null && System.currentTimeMillis() - CacheManager.getInstance().getCacheTime(url) < cacheTime){
//                //使用缓存
//                isRefresh = false;
//                //获取缓存bean
//                T cacheT = gson.fromJson(cache,new TypeToken<T>() {}.getType());
//                //渲染数据
//                onNext(cacheT);
//                //停止网络请求数据
//                dispose();
//            }

            //访问网络

        } catch (Exception e){
            Log.i("testCache","test"+Log.getStackTraceString(new Throwable()));
        }



    }

    @Override
    public void onNext(T t) {

        //如果是刷新就存入缓存
        if (isRefresh){
            CacheManager.getInstance().saveCache(gson.toJson(t.toString()),url);
        }

        if (mBaseView != null && isPageLoading) {
            mBaseView.hideLoading();
        }
    }

    @Override
    public void onComplete() {
        if (mBaseView != null && isHideLoading) {
            mBaseView.hideDialogLoading();
        }
        if (mBaseView != null && isPageLoading) {
            mBaseView.hideLoading();
        }
    }

    @Override
    public void onError(Throwable mThrowable) {
        if (mBaseView == null) {
            return;
        }
        if (isHideLoading) {
            mBaseView.hideDialogLoading();
        }
        if (mBaseView != null && isPageLoading) {
            mBaseView.hideLoading();
        }
        ApiException exception = (ApiException) mThrowable;
        if(exception.getCode()== CustomException.NETWORK_ERROR){
            RetrofitServiceManager.getInstance().refreshHost(mBaseView.getMContext());
        }
        if (exception.getCode() == 401) {
            mBaseView.showLogin();
        }
        if (isPageLoading) {
            if (exception.getCode() == 401) {
                mBaseView.showLogin();
            } else {
                mBaseView.showError();
            }
        } else {
            if (apiExceptionConsumer != null) {
                try {
                    apiExceptionConsumer.accept(exception);
                } catch (Exception e) {

                }
            } else {
                if (exception.getCode() == 401) {
                    mBaseView.showLogin();
                } else if (exception.getCode() >= 500) {
                    //展示网络错误页面
                    mBaseView.showMessage("网络异常");
                } else {
                    //40x提示返回的msg
                    mBaseView.showMessage(exception.getDisplayMessage());
                }
            }
        }
    }
}
